<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/31
 * Time: 15:34
 */

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\HouseProperty;
use app\common\utils\Constant;
use app\common\utils\SmsUtils;
use app\common\utils\WeChatUtils;
use app\index\logic\HousePropertyLogic;
use app\index\logic\UserLogic;
use Exception;
use think\Config;
use think\Log;

class User extends Frontend
{

    public function test(){
        $we_chat = Config::get('WeChat');
        $utils = new WeChatUtils($we_chat['app_id'], $we_chat['app_secret'], $we_chat['token']);
        if (!isset($_GET['code'])) {
            //如果没有收到CODE返回值，就发送请求
            $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
            $utils->getUserCodeByUrl('',$baseUrl);
            var_dump($baseUrl);
        }else{
            var_dump($_GET['code']);
        }
    }
    //4d633dc853f55f36918bfa87787f7f

    protected function returnJson($msg='',$code=1,$data=[]){
        return json(['code'=>$code,'msg'=>$msg,'data'=>$data]);
    }


    /**
     * 根据token获取用户信息
     * @return \think\response\Json
     */
    public function ajax_get_user_info_token(){
        $token = $this->request->param('token');
        $logic = new UserLogic();
        $result = $logic->getUserByToken($token);
        if($result['status'] == 1){
            return $this->returnJson('',Constant::CODE_SUCCESS,$result['data']);
        }else{
            return $this->returnJson('获取失败!',Constant::CODE_ERROR);
        }
    }

    /**
     * ajax根据微信code获取用户信息
     * @return \think\response\Json
     */
    public function ajax_get_user_info(){
        $code = $this->request->param('code');
        $logic = new UserLogic();
        $result = $logic->getUserByCode($code);
        if($result['status'] == 1){
            return $this->returnJson('',Constant::CODE_SUCCESS,$result['data']);
        }else{
            return $this->returnJson('获取失败!',Constant::CODE_ERROR,$result['error']);
        }
    }

    /**
     * ajax绑定用户信息
     */
    public function ajax_bind_user_info(){
        $result = $this->check(false);
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $params = $this->request->post();
        if(empty($params['code'])){
            return $this->returnJson('请填写验证码', Constant::CODE_ERROR, []);
        }

        $re = UserLogic::getInstance()->bindUserInfo($params, $result['user_id']);
        return $this->returnJson($re['msg'], $re['status'], []);
    }

    /**
     * ajax获取房产信息列表
     */
    public function ajax_get_house_property(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $data = HousePropertyLogic::getInstance()->getHousePropertyListByUserId($result['user_id']);
        if(empty($data)){
            return $this->returnJson('无数据', Constant::CODE_ERROR, []);
        }
        return $this->returnJson('获取成功', Constant::CODE_SUCCESS, $data);
    }

    /**
     * ajax获取验证码
     */
    public function ajax_get_code(){
        $result = $this->check(false);
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $phone = $this->request->param('phone');
        $user_id = $result['user_id'];
        $logic = new UserLogic();
        $result = $logic->getVerifyCode($phone,$user_id);
        $code = 0;
        $msg = '';
        $data = [];
        switch ($result['status']){
            case 0:
                $code = Constant::CODE_ERROR;
                $msg = '获取验证码失败';
                $data = $result['error'];
                break;
            case 1:
                $code = Constant::CODE_SUCCESS;
                $msg = '验证码已发送';
                break;
            case 2:
                $code = Constant::CODE_ERROR;
                $msg = $result['msg'];
                break;
        }
        return $this->returnJson($msg,$code,$data);
    }

    /**
     * 判断用户是否绑定业主
     */
    public function ajax_is_bind(){
        $result = $this->check(false);
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $owner = model('Owner')->where(array('user_id' => $result['user_id']))->find();
        if (!$owner) {
            return $this->returnJson('未绑定信息',Constant::CODE_UNBOUNDED);
        }
        else{
            return $this->returnJson('已绑定信息',Constant::CODE_SUCCESS);
        }
    }

    /**
     * ajax获取业主信息
     * @return \think\response\Json
     */
    public function ajax_get_owner_info(){
        $result = $this->check(false);
        if(!$result['success']) {
            return $this->returnJson($result['msg'],$result['code']);
        }
        $owner = model('Owner')->where(array('user_id' => $result['user_id']))->find();
        if (!$owner) {
            return $this->returnJson('未绑定信息',Constant::CODE_UNBOUNDED);
        }
        $id_card = $owner['id_card'];
        $phone = $owner['phone'];
        $id_card = strlen($id_card)==15?substr_replace($id_card,"*******",4,7):(strlen($id_card)==18?substr_replace($id_card,"**********",4,10):"身份证位数不正常！");
        $phone = substr($phone,0,3)."******".substr($phone,9,2);
        $owner['id_card'] = $id_card;
        $owner['phone'] = $phone;
        return $this->returnJson('',Constant::CODE_SUCCESS,$owner);
    }


    /**
     * ajax绑定用户
     * @return \think\response\Json
     */
    /*public function ajax_bind_user(){
        $result = $this->check();
        if(!$result['success']){
            return $this->returnJson($result['msg'],$result['code']);
        }
        $last_user_id = $this->request->post('user_id');
        $user_id = $result['user_id'];
        $logic = new UserLogic();
        $result = $logic->bindUser($last_user_id,$user_id);
        $code = 0;
        $msg = '';
        $data = [];
        switch ($result['status']){
            case 0:
                $code = Constant::CODE_ERROR;
                $msg = '绑定失败！';
                $data = $result['error'];
                break;
            case 1:
                $code = Constant::CODE_SUCCESS;
                $msg = '绑定成功！';
                break;
            case 2:
                $code = Constant::CODE_ERROR;
                $msg = $result['msg'];
                break;
        }
        return $this->returnJson($msg,$code,$data);
    }*/
}